package com.weakteam.orderonline.administratorconsole.web;

import com.weakteam.orderonline.dataresource.domain.Menu;
import com.weakteam.orderonline.dataresource.service.MenuService;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Nail on 09.03.14.
 */

@Controller
public class MenuNetService {

    @Autowired
    MenuService menuService;

    private static final Logger log = LoggerFactory.getLogger(MenuNetService.class);

    private static void initJSONHeader(final HttpServletResponse httpResponse){
        httpResponse.setHeader("Connection", "close");
        httpResponse.setHeader("Content-Type", "application/json");
        httpResponse.setHeader("Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
        httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        httpResponse.setHeader("Pragma", "no-cache");
        httpResponse.setCharacterEncoding("UTF-8");
    }

    @RequestMapping(value = "/getMenuData")
    public void getMenuData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            List<Menu> menus = menuService.getAll();
            out.print("[");
            for (Iterator<Menu> it = menus.iterator(); it.hasNext();) {
                Menu menu = it.next();
                out.print(menu.getJSON());
                if (it.hasNext()) out.println(",");
            }
            out.print("]");
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("getMenuData {}", ex);
        }
    }

    @RequestMapping(value = "/saveMenuDataOne")
    public void saveMenuDataOne(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            String menuJson = httpRequest.getParameter("menu");

            Menu menu = new Menu();
            menu.setJSON(menuJson);
            this.menuService.save(menu);

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("");
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("saveMenuDataOne {}", ex);
        }
    }

    @RequestMapping(value = "/saveMenuData")
    public void saveMenuData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            String json = httpRequest.getParameter("data");

            JSONParser parser = new JSONParser();
            JSONArray jData = (JSONArray)parser.parse(json);
            for(Iterator<JSONObject> it = jData.iterator(); it.hasNext();){
                JSONObject jObj = it.next();
                Menu menu = new Menu();
                menu.setJSON(jObj);
                this.menuService.save(menu);
            }

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("");
            out.flush();
            out.close();
        } catch (ParseException ex) {
            log.error("saveMenuData parse exception {}", ex);
        } catch (IOException ex) {
            log.error("saveMenuData io exception {}", ex);
        }
    }

    @RequestMapping(value = "/deleteMenuData")
    public void deleteMenuData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            Integer id = Integer.parseInt(httpRequest.getParameter("id"));

            this.menuService.delete(id);

            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("");
            out.flush();
            out.close();

        } catch (IOException ex) {
            log.error("deleteMenuData {}", ex);
        }
    }

}